#coding=utf-8
from sqlalchemy import *
from uliweb.orm import *
from uliweb.utils.pyini import Ini
from sqlalchemy.sql import select
import time, sys
sys.path.insert(0, '../apps')
from charts.models import *

db = get_connection('sqlite:///../database.db', debug=False)
meta = MetaData(db)
db.metadata.reflect()

settings = Ini('../apps/settings.ini')


#字典化条线
tx = dict(settings.PARA.TX)

#准开发需求相关报表数据处理
req_table = db.metadata.tables['requirement']                                                        
tx_table = db.metadata.tables['requirments_tx_count']                      

#计算                                  
s = select([func.substr(req_table.c.req_id,0,2),func.count(req_table.c.id)]).group_by(func.substr(req_table.c.req_id,0,2))
rs = s.execute()
data = []                                                                
for row in rs:                                 
    tx_co = Requirments_tx_count(tx_id=row[0], tx_count=row[1])
    data.append(tx_co)
    #tx_co.save()
    #x = tx_table.insert().values(id=i, tx_id=row[0], tx_count=row[1])
    #i+=1
    #db.execute(x)
for row in data:
    row.save()